﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class nhapkhachhang : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("khachthue");
        private DataTable dt1 = new DataTable("dichvu");
        private DataTable dt2 = new DataTable("dkdichvu");
        private DataTable dt3 = new DataTable("phong");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open(); //mo ket noi

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public nhapkhachhang()
        {
            InitializeComponent();
            rdnam.Checked = true;
            
        }

       

        private Boolean kiemtra()
        {
            kt = true;

            if (txtcmnd.Text == "")
            {
                MessageBox.Show("số CMND không được để trống !", "thông báo", MessageBoxButtons.OK);
                kt = false;
                txtcmnd.Text = "0000000000000";
                txtcmnd.Focus();
            }
            else if (txtten.Text == "" | txtho.Text == "")
            {
                MessageBox.Show("Tên khách không được để trống!", "thông báo", MessageBoxButtons.OK);
                kt = false;
                txtten.Focus();
            }
            return kt;
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  * from khachthue ";
            da.SelectCommand = command;
            da.Fill(dt);

            command.CommandText = @"select * from dichvu";
            da.SelectCommand = command;
            da.Fill(dt1);
            ListBox list = ckdkdichvu as ListBox;
            list.DataSource = dt1;
            list.DisplayMember = "dvtendichvu";
            list.ValueMember = "dvid";

            command.CommandText = @"select * from dkdichvu";
            da.SelectCommand = command;
            da.Fill(dt2);

            command.CommandText = @"select * from phong where pstatus = '0'";
            da.SelectCommand = command;
            da.Fill(dt3);
            cbbphong.DataSource = dt3;
            cbbphong.DisplayMember = "pid";
            cbbphong.ValueMember = "pid";
        }

        
        
        private void btncancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();
                row["ktcmnd"] = Convert.ToInt32(txtcmnd.Text);
                row["ktten"] = txtten.Text;
                row["ktho"] = txtho.Text;
                if (rdnam.Checked)
                {
                    row["gioitinh"] = 1;
                }
                else row["gioitinh"] = 0;
                row["ktsdt"] = Convert.ToInt32(txtsdt.Text);
                row["ngaysinh"] = dtngaysinh.Value;
                row["quequan"] = txtquequan.Text;
                row["ktsophong"] = cbbphong.SelectedValue;
                row["ktngaythue"] = dtngaythue.Value;
                row["ktngayhethan"] = dtngayhethan.Value;
                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = conn;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert into khachthue values(@ktcmnd,@ktten,@ktho,@gioitinh,@ktsdt,@ngaysinh,@quequan,@ktsophong,@ktngaythue,@ktngayhethan);";
                commandInsert.Parameters.Add("@ktcmnd", SqlDbType.Int, 20, "ktcmnd");
                commandInsert.Parameters.Add("@ktten", SqlDbType.NVarChar, 30, "ktten");
                commandInsert.Parameters.Add("@ktho", SqlDbType.NVarChar, 30, "ktho");
                commandInsert.Parameters.Add("@gioitinh", SqlDbType.Int, 30, "gioitinh");
                commandInsert.Parameters.Add("@ktsdt", SqlDbType.Int, 20, "ktsdt");
                commandInsert.Parameters.Add("@ngaysinh", SqlDbType.DateTime, 30, "ngaysinh");
                commandInsert.Parameters.Add("@quequan", SqlDbType.NVarChar, 50, "quequan");
                commandInsert.Parameters.Add("@ktsophong", SqlDbType.Int, 20, "ktsophong");
                commandInsert.Parameters.Add("@ktngaythue", SqlDbType.DateTime, 30, "ktngaythue");
                commandInsert.Parameters.Add("@ktngayhethan", SqlDbType.DateTime, 30, "ktngayhethan");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                dt.Clear();
                for (int i = 0; i < ckdkdichvu.CheckedItems.Count; i++)
                {
                    DataRow row1 = dt2.NewRow();
                    row1["dkdvsophong"] = cbbphong.SelectedValue;
                    row1["dvid"] = ckdkdichvu.CheckedIndices[i];
                    String t = ckdkdichvu.CheckedIndices[i].ToString();
                    dt2.Rows.Add(row1);
                    SqlCommand commandInsertDv = new SqlCommand();
                    commandInsertDv.Connection = conn;
                    commandInsertDv.CommandType = CommandType.Text;
                    commandInsertDv.CommandText = @"Insert into dkdichvu values(@dkdvsophong,@dvid)";
                    commandInsertDv.Parameters.Add("@dkdvsophong", SqlDbType.Int, 20, "dkdvsophong");
                    commandInsertDv.Parameters.Add("@dvid", SqlDbType.Int, 20, "dvid");
                    da.InsertCommand = commandInsertDv;
                    da.Update(dt2);
                  }


               
                DataRow row2 = dt3.Select("pid = " + cbbphong.SelectedValue)[0];
                row2.BeginEdit();
                row2["pid"] = cbbphong.SelectedValue;
                if (rddthang.Checked)
                {
                    row2["pthanhtoan"] = 1;
                }
                else if (rddquy.Checked)
                {
                    row2["pthanhtoan"] = 2;
                }
                else
                {
                    row2["pthanhtoan"] = 3;
                }
                row2.EndEdit();
                SqlCommand commandInsertPhong = new SqlCommand();
                commandInsertPhong.Connection = conn;
                commandInsertPhong.CommandType = CommandType.Text;
                commandInsertPhong.CommandText = @"Update phong set pthanhtoan=@pthanhtoan,pstatus=@pstatus where pid =@pid ";
                commandInsertPhong.Parameters.Add("@pthanhtoan", SqlDbType.Int, 20, "pthanhtoan");
                commandInsertPhong.Parameters.Add("@pid", SqlDbType.Int, 20, "pid");
                commandInsertPhong.Parameters.Add("@pstatus", SqlDbType.Int, 20).Value = '1';
                da.UpdateCommand = commandInsertPhong;
                da.Update(dt3);
                MessageBox.Show("Bạn đã nhập  thành công", "thông báo", MessageBoxButtons.OK);
                Close();
                Dispose();
                disconnect();
            }
        }

        private void nhapkhachhang_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }

        private void ckball_CheckedChanged(object sender, EventArgs e)
        {
            if (ckball.Checked) 
            {
                for (int i = 0; i < ckdkdichvu.Items.Count; i++)
                
                    ckdkdichvu.SetItemChecked(i, true);
                
            }else
            {
                foreach (int i in ckdkdichvu.CheckedIndices)
                    ckdkdichvu.SetItemChecked(i, false);
     
            }
            
        }       
    }
}
